# -*- coding: utf-8 -*-
# author: ting.wei
# time: 2022/10/31
import time

from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.extensions.android.nativekey import AndroidKey
from proj_spec.CLUE.mobile_po.clue_app_base_page import CLUEAppBasePage
from proj_spec.CLUE.mobile_po.company_detail_page import CompanyDetailPage
from selenium.webdriver.common.keys import Keys


class PotentialCustomerPage(CLUEAppBasePage):
    """
    潜客营销页面
    """

    _prior_search_locator_android = (AppiumBy.XPATH, "//*[@resource-id='search.potential']/child::*[1]/child::*[2]/child::*[1]")
    _prior_search_locator_ios = (AppiumBy.IOS_CLASS_CHAIN, '**/XCUIElementTypeOther[`label contains "pages/potential/index"`]/XCUIElementTypeSearchField')

    _post_search_locator_android = (AppiumBy.XPATH, "//*[@resource-id='search.potential']/child::*[1]/child::*[1]/child::*[1]")
    _post_search_locator_ios = (AppiumBy.IOS_CLASS_CHAIN, '**/XCUIElementTypeOther[`label contains "pages/potential/index"`]/XCUIElementTypeSearchField')

    _search_results_count_android = (AppiumBy.XPATH, "//*[@text='为您找到']/following-sibling::*[1]")
    _search_results_count_ios = (AppiumBy.XPATH, '//XCUIElementTypeStaticText[@name="为您找到"]/following-sibling::XCUIElementTypeStaticText[1]')

    _main_page_locator_android = (AppiumBy.ANDROID_UIAUTOMATOR,'UiSelector().text("首页")')
    _main_page_locator_ios = (AppiumBy.IOS_PREDICATE, 'label == "首页" AND type == "XCUIElementTypeButton"')

    _confirm_btn_locator_android = (AppiumBy.ANDROID_UIAUTOMATOR, 'UiSelector().text("确认")')
    _confirm_btn_locator_ios = (AppiumBy.IOS_PREDICATE, 'label == "确认"')

    def to_com_detail(self, company_name):
        if self.os == 'android':
            potential_customer_locator = (AppiumBy.XPATH, f"//*[contains(@text,'pages/potential/index')]//*[@text='{company_name}']/../../..[not(@resource-id='search.potential')]")
        else:
            potential_customer_locator = (AppiumBy.IOS_PREDICATE, f'label == "{company_name}"')
        self.find_element_and_click(potential_customer_locator)
        return CompanyDetailPage(self.driver)


    def search_company(self, keyword):
        """
        在搜索框中输入企业名称
        return: 搜索结果总数
        """
        search_box = self.find_element(getattr(self, '_prior_search_locator_' + self.os))
        search_box.click()
        search_box.send_keys(keyword)
        if self.os == 'android':
            self.driver.press_keycode(AndroidKey.ENTER)
            return self.find_element(getattr(self, '_search_results_count_' + self.os)).text
        else:
            search_box.send_keys(Keys.ENTER)
            return self.find_element(getattr(self, '_search_results_count_' + self.os)).get_attribute("name")


    def clear_search(self):
        search_box = self.find_element(getattr(self, '_post_search_locator_' + self.os))
        search_box.click()
        if self.os == 'android':
            search_box.clear()
            self.driver.press_keycode(AndroidKey.ENTER)
        else:
            # 输入框内的数据过长时，clear无法完全清空，需要重复调用clear
            while search_box.get_attribute("value") is not None and len(search_box.get_attribute("value")) > 0:
                search_box.clear()
            search_box.send_keys(Keys.ENTER)

    def to_main(self):
        self.find_element_and_click(getattr(self, '_main_page_locator_' + self.os))

    def delete_potential_customer(self, company_name):
        """
        左滑结束营销
        """
        width = self.driver.get_window_size()['width']
        potential_locator = (AppiumBy.XPATH, f"//*[contains(@text,'pages/potential/index')]//*[@text='{company_name}']/../../..[not(@resource-id='search.potential')]")
        self.swipe_by_element(potential_locator, -width * 0.4, 0, 500)
        time.sleep(1)
        end_marketing = self.driver.find_element(AppiumBy.XPATH,f"//*[contains(@text,'pages/potential/index')]//*[@text='{company_name}']/../../../..//*[@text='结束营销']")
        end_marketing.click()
        self.find_element_and_click(getattr(self, '_confirm_btn_locator_' + self.os))